 <?php

class AccountinfoController extends Zend_Controller_Action
{
	private $_auth;
	private $_acl;
    private $_params;
    private $_memcache;
    private $_my_mem;
    public function init(){
        $acl                = new My_Acl();
        $this->_acl         = $acl;
        $auth               = $acl->checkAllowed($this->_request);
        $this->_auth        = $auth->getIdentity();
        $this->view->auth   = $this->_auth;
        $this->_acl         = $acl;
        $this->view->acl    = $this->_acl;
        $this->_params      = $this->_request->getParams();
        
        $memcache = new Memcache;
        $memcache->connect('localhost', MEMCACHE_PORT) or die ("Could not connect");
        $this->_memcache = $memcache;
        $_my_mem = new My_Mem();
        $this->_my_mem = $_my_mem;
    }
    public function indexAction(){
 
        # get time start and time end of statistic
        $_params            = $this->_params;
        $gameId             = $_params['game'];
        $this->view->gameId = $gameId;
        $modelGame          = new Default_Model_Game();
        $gameCheck          = $modelGame->getDataById($gameId)->toArray();
        if($gameCheck){
            $this->view->gameCheck = $gameCheck;
            //get server List       
            if(file_exists(CONFIG_PATH . '/'.$gameId.'.ini')){
                #config ssgroup and config game
                $configGame = new Zend_Config_Ini(CONFIG_PATH . '/'.$gameId.'.ini', APPLICATION_ENV);
                if($gameId!='ss31'){
                $modelRole          = new Default_Model_Role();
                $roleList           = $modelRole->getDataByUserIdGameId($this->_auth['userId'],$gameId)->toArray();
                $channelDisabled    = array();
                foreach($roleList as $row){
                    if(isset($row['channelDisabled']) && $row['channelDisabled']){
                        if(is_array(json_decode($row['channelDisabled']))){
                            $channelDisabled    = array_merge($channelDisabled,json_decode($row['channelDisabled']));
                        }
                    }
                }
                if(isset($configGame->channel)){
                    $channelGame = array();
                    foreach($configGame->channel->toArray() as $key=>$value){
                        $channelGame[] = $key;
                    }
                }
                
                $arrChannel       = $configGame->channel->toArray();
                foreach($arrChannel as $key=>$row){
                    if(!isset($channelDisabled[0]) || (isset($channelDisabled[0]) && !in_array($key,$channelDisabled))){
                        
                    }else{
                        unset($arrChannel[$key]);
                    }
                }
                //loai bo cac channel nguoi dung khong duoc phep xem
                $this->view->arrChannel = $arrChannel;
                
            }
            }
            if(file_exists(CONFIG_PATH2 . '/'.$gameId.'.ini')){
                $configGame = new Zend_Config_Ini(CONFIG_PATH2 . '/'.$gameId.'.ini', APPLICATION_ENV);
                if(isset($_params['bntSubmit']) && $_params['bntSubmit'] == 'Tìm'){
                    $username = '';
                    $channel = '';
                    $serverId = '';
                    if(!isset($_params['username']) || !$_params['username']){
                        $this->view->mess = 'Vui lòng nhập username!';
                        $this->view->flag = 'alert';
                        return;
                    }
                    $username               = trim($_params['username']);
                    $this->view->username   = $username;
                    if(isset($_params['channel'])){
                        $channel                = trim($_params['channel']);
                        $this->view->channel    = $channel;
                        if(isset($arrChannel) && !isset($arrChannel[$channel])){
                            $this->view->mess = 'Vui lòng chọn lại channel!';
                            $this->view->flag = 'alert';
                            return;
                        }
                    }
                    $getProfile = new My_API_getProfile();
                    $dataUser[$channel] = $getProfile->getdataUserID(array('userList'=>$username,'channel'=>$channel)); 
                    foreach($dataUser[$channel] as $key => $value)
                    {
                        if(!isset($value['mobile']) || !$value['mobile'])
                        {
                            $this->view->mess = "Không tìm thấy user {$username}!";
                            $this->view->flag = 'alert';
                            return;
                        }
                    }
                    $this->view->dataUser = $dataUser;
                }
            }else{
                $this->view->mess = 'Chưa có setting cho game!';
                $this->view->flag = 'error';
                $this->view->repair = 1;
            }
        }
    }
}